package com.demo2do.fms.admin.service.supplier.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.vehicle.RemoteTransMeansDao;
import com.demo2do.fms.admin.dao.vehicle.VehicleSourceDao;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.VehicleDriverRelationService;
import com.demo2do.fms.entity.driver.Driver;
import com.demo2do.fms.entity.vehicle.source.Vehicle;
import com.demo2do.fms.entity.vehicle.source.VehicleSource;

/**
 *
 * @author Ark
 *
 */
@Service("vehicleDriverRelationService")
public class VehicleDriverRelationServiceImpl implements VehicleDriverRelationService {
    
    @Dao
    private VehicleSourceDao vehicleSourceDao;
    
    @Dao
    private RemoteTransMeansDao remoteTransMeansDao;
    
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.VehicleDriverRelationService#list(java.lang.Long)
     */
//    public List<VehicleDriverRelation> list(Long vehicleId) {
//    	
//        VehicleSource vehicleSource = genericDaoSupport.load(VehicleSource.class, vehicleId);
//        
//        if (StringUtils.isNotBlank(vehicleSource.getCode())) {
//        	return vehicleDriverRelationDao.list(vehicleSource.getCode());
//        }
//        return Collections.emptyList();
//        
//    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.VehicleDriverRelationService#create(com.demo2do.fms.entity.driver.Driver, java.lang.Long, java.lang.Long)
     */
    public void create(Driver driver, Long vehicleId, Long operator) {
        
        Vehicle vehicle = genericDaoSupport.get(Vehicle.class, vehicleId);
        if (vehicle == null) {
            throw BusinessException.newInstance("500", "该车辆为无效车辆");
        }
        
        // 1. check vehicle whether in vehicleSource
        VehicleSource source = vehicleSourceDao.loadByCode(vehicle.getCode());
        if (source == null || !source.couldBind()) {
        	throw BusinessException.newInstance("500", "该车辆没有在FMS+系统中引入并审核，请重新引入");
        }
        
        // 2. create relation by driver'duty
//        if (10 == driver.getDutyId()) {
//            
//            VehicleDriverRelation relation = new VehicleDriverRelation(vehicle.getCode(), driver.getId(), null);
//            genericDaoSupport.save(relation);
//            
//        } else if (11 == driver.getDutyId()) {
//            
//            VehicleDriverRelation relation = new VehicleDriverRelation(vehicle.getCode(), null, driver.getId());
//            genericDaoSupport.save(relation);
//            
//        } else {
//            throw BusinessException.newInstance("500", "无效司机");
//        }
        
        // 3. update driver info
        genericDaoSupport.update(driver.bind(vehicleId).operate(new Date(), operator));
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.VehicleDriverRelationService#edit(com.demo2do.fms.entity.driver.Driver, java.lang.Long, java.lang.Long)
     */
    public void edit(Driver driver, Long vehicleId, Long operator) {
        
        // 1.1 get relation
//        Vehicle oldVehicle = genericDaoSupport.load(Vehicle.class, driver.getVehicleId());
//        VehicleDriverRelation relation = null;
//        if (10 == driver.getDutyId()) {         // 主驾
//            relation = vehicleDriverRelationDao.loadByDriver(oldVehicle.getCode(), driver.getId());
//        } else if (11 == driver.getDutyId()) {  // 第一副驾
//            relation = vehicleDriverRelationDao.loadByCopilot(oldVehicle.getCode(), driver.getId());
//        } else {
//            throw BusinessException.newInstance("500", "无效司机");
//        }
//        
//        // 1.2 update relation
//        Vehicle newVehicle = genericDaoSupport.load(Vehicle.class, vehicleId);
//        genericDaoSupport.update(relation.editVehicle(newVehicle.getCode()));
        
        // 2. update driver info
        genericDaoSupport.update(driver.bind(vehicleId).operate(new Date(), operator));
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.VehicleDriverRelationService#edit(com.demo2do.fms.entity.vehicle.VehicleDriverRelation)
     */
//    public void edit(VehicleDriverRelation relation) {
//        genericDaoSupport.update(relation);
//    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.DriverService#unbind(com.demo2do.fms.entity.driver.Driver)
     */
    public void unbind(Driver driver, Login login) {
        
        Long operator = login.getId();
        Date now = new Date();
        
        // 0. prepare data
//        Vehicle vehicle = genericDaoSupport.load(Vehicle.class, driver.getVehicleId());
        
        // 1.1 set vehicleId to null in driver
        genericDaoSupport.update(driver.unbind(operator, now));
        
        // 1.2 update relation
//        VehicleDriverRelation relation = null;
//        if (10 == driver.getDutyId()) {         // 主驾
//            relation = vehicleDriverRelationDao.loadByDriver(vehicle.getCode(), driver.getId());
//            genericDaoSupport.update(relation.editDriver(null));
//        } else if (11 == driver.getDutyId()) {  // 第一副驾
//            relation = vehicleDriverRelationDao.loadByCopilot(vehicle.getCode(), driver.getId());
//            genericDaoSupport.update(relation.editCopilot(null));
//        } else {
//            throw BusinessException.newInstance("500", "无效司机");
//        }
//        
//        // 1.3 if relation invalid, remove
//        if (relation != null && relation.inValid()) {
//        	genericDaoSupport.update(relation.disable());
//        }
        
//        // 2. lock vehicle
//        LockType type = LockType.SHIPMENT_EXCEPTION;
//        String reason = "车辆没有驾驶员与之关联";
//        VehicleSource vehicleSource = vehicleSourceDao.loadByCode(vehicle.getCode());
//        if (vehicleSource == null) {
//            throw BusinessException.newInstance("500", "在FMS中未找到对应车辆，请检查车辆是否已同步");
//        }
//        genericDaoSupport.update(vehicleSource.doLock(reason, type, operator));
//        
//        String vehicleCode = vehicleSource.getCode();
//        RemoteTransMeans remoteTransMeans = remoteTransMeansDao.loadByCode(vehicleCode);
//        
//        if(remoteTransMeans == null) {
//            throw new BusinessException("锁车数据信息为空");
//        }
//        
//        if(remoteTransMeans.isAvaliable()) {
//            genericDaoSupport.update(remoteTransMeans.lock(reason, type));
//        } else {
//            throw new BusinessException("MDM中该车辆为非正常车辆");
//        }
//        
//        // 创建锁车记录
//        VehicleSourceLockRecord record = new VehicleSourceLockRecord(vehicleSource, LockStatus.LOCKED, operator);
//        genericDaoSupport.save(record);
        
    }

}